<template>
	<div class="login-container">
		<headerNav :showBtn="false" />

		<div class="login-content">
			<div class="login-left">
				<img src="../../assets/bg1.png" alt="logo" class="login-image" />
				<div class="login-slogan">
					<h2>欢迎使用我们的平台</h2>
					<p>一站式解决方案，为您提供高效便捷的服务体验</p>
				</div>
			</div>
			<div class="login-right">
				<div class="login-box">
					<h2 class="login-title">{{ $t('message.title') }}</h2>
					<form @submit.prevent="handleLogin" class="login-form">
						<div class="form-group">
							<input type="text" v-model="state.ruleForm.userName" placeholder="请输入邮箱/手机号/用户名" class="form-input" required />
						</div>
						<div class="form-group">
							<input type="password" v-model="state.ruleForm.password" placeholder="请输入密码" class="form-input" required />
							<a @click="handleForget" class="forgot-password">忘记密码？</a>
						</div>
						<button type="submit" class="login-button">登录</button>
						<div class="login-agreement">
							<span
								>登录视为您已阅读并同意平台<span @click="handleService" class="register-link">服务条款</span>和
								<span @click="handlePrivacy" class="register-link">隐私政策</span>
							</span>
						</div>
					</form>
					<div class="login-footer">
						<!-- <div class="divider">或</div>
						<div class="social-login">
							<button class="social-button wechat"><i class="icon-wechat"></i> 微信登录</button>
							<button class="social-button qq"><i class="icon-qq"></i> QQ登录</button>
						</div> -->
						<div class="register-hint">
							您还没有账号吗？
							<span @click="handleRegister" class="register-link">立即注册</span>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script setup lang="ts" name="layoutBreadcrumbUser">
import Cookies from 'js-cookie';
import { useI18n } from 'vue-i18n';
import { storeToRefs } from 'pinia';
import { ElMessage } from 'element-plus';
import { Session } from '/@/utils/storage';
import { useRoute, useRouter } from 'vue-router';
import { formatAxis } from '/@/utils/formatTime';
import { useThemeConfig } from '/@/stores/themeConfig';
import { initBackEndControlRoutes } from '/@/router/backEnd';
import { initFrontEndControlRoutes } from '/@/router/frontEnd';
import { reactive, defineAsyncComponent, onMounted, computed } from 'vue';

const headerNav = defineAsyncComponent(() => import('../../components/headerNav/index.vue'));
// 定义变量内容
const { t } = useI18n();
const route = useRoute();
const router = useRouter();
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
const state = reactive({
	ruleForm: {
		userName: 'admin',
		password: '123456',
		code: '1234',
	},
	loading: {
		signIn: false,
	},
});

// 忘记密码
const handleForget = () => {
	router.push({ path: '/forgotPassword' });
};

// 服务条款
const handleService = () => {
	const url = new URL(`/#/termService`, window.location.origin).toString();
	window.open(url, '_blank');
};

// 隐私政策
const handlePrivacy = () => {
	const url = new URL(`/#/privacyPolicy`, window.location.origin).toString();
	window.open(url, '_blank');
};

// 时间获取
const currentTime = computed(() => {
	return formatAxis(new Date());
});

// 登录
const handleLogin = async () => {
	state.loading.signIn = true;
	// 存储 token 到浏览器缓存
	Session.set('token', Math.random().toString(36).substr(0));
	// 模拟数据，对接接口时，记得删除多余代码及对应依赖的引入。用于 `/src/stores/userInfo.ts` 中不同用户登录判断（模拟数据）
	Cookies.set('userName', state.ruleForm.userName);

	if (!themeConfig.value.isRequestRoutes) {
		// 前端控制路由，2、请注意执行顺序
		const isNoPower = await initFrontEndControlRoutes();
		signInSuccess(isNoPower);
	} else {
		// 模拟后端控制路由，isRequestRoutes 为 true，则开启后端控制路由
		// 添加完动态路由，再进行 router 跳转，否则可能报错 No match found for location with path "/"
		const isNoPower = await initBackEndControlRoutes();
		// 执行完 initBackEndControlRoutes，再执行 signInSuccess
		signInSuccess(isNoPower);
	}
};

// 登录成功后的跳转
const signInSuccess = (isNoPower: boolean | undefined) => {
	if (isNoPower) {
		const promptText = t('message.promptText');
		ElMessage.warning(`${promptText}`);
		Session.clear();
	} else {
		// 初始化登录成功时间问候语
		let currentTimeInfo = currentTime.value;
		// 登录成功，跳到转首页
		// 如果是复制粘贴的路径，非首页/登录页，那么登录成功后重定向到对应的路径中
		if (route.query?.redirect) {
			const redirectPath = Array.isArray(route.query?.redirect) ? route.query.redirect[0] || '/' : route.query?.redirect || '/';

			router.push({
				path: redirectPath,
				query: Object.keys(route.query?.params || {}).length > 0 ? JSON.parse(route.query.params as string) : {},
			});
		} else {
			router.push('/smartLargeScreen/mainStationScreenNews');
		}
		// 登录成功提示
		const signInText = t('message.signInText');
		ElMessage.success(`${currentTimeInfo}，${signInText}`);
		// 添加 loading，防止第一次进入界面时出现短暂空白
		// NextLoading.start();
	}
	state.loading.signIn = false;
};

//  注册
const handleRegister = () => {
	router.push({ path: '/register' });
};

onMounted(() => {});
</script>

<style scoped lang="scss">
.login-container {
	height: 100vh;
	display: flex;
	flex-direction: column;
	background-color: #f5f7fa;
	font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
}

.login-content {
	display: flex;
	flex: 1;
	align-items: stretch;
}

.login-left {
	flex: 1;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	background: linear-gradient(135deg, #1890ff, #096dd9);
	color: white;
	position: relative;
	overflow: hidden;
}

.login-image {
	max-width: 75%;
	max-height: 50vh;
	margin-bottom: 2rem;
	object-fit: contain;
}

.login-slogan {
	text-align: center;
	h2 {
		font-size: 1.5rem;
		font-weight: 500;
		margin-bottom: 1rem;
	}
	p {
		font-size: 1rem;
		opacity: 0.9;
	}
}

.login-right {
	width: 35rem;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 3rem;
	background-color: #fff;
}

.login-box {
	width: 100%;
	max-width: 30rem;
}

.login-title {
	font-size: 1.8rem;
	color: #000;
	text-align: center;
	margin-bottom: 2rem;
	font-weight: 500;
}

.login-form {
	display: flex;
	flex-direction: column;
	gap: 1.8rem;
}

.form-group {
	position: relative;
}

.form-input {
	width: 100%;
	padding: 0.8rem 1rem;
	border: 1px solid #d9d9d9;
	border-radius: 4px;
	font-size: 1rem;
	outline: none;
	transition: all 0.3s;
	box-sizing: border-box;
	height: 3.5rem;

	&:focus {
		border-color: #1890ff;
		box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
	}

	&::placeholder {
		color: #bfbfbf;
	}
}

.forgot-password {
	position: absolute;
	right: 0;
	top: 0;
	font-size: 1rem;
	color: #1890ff;
	text-decoration: none;
	line-height: 3.5rem;
}

.login-button {
	width: 100%;
	padding: 1rem;
	background-color: #1890ff;
	color: white;
	border: none;
	border-radius: 4px;
	font-size: 1.5rem;
	font-weight: 500;
	cursor: pointer;
	transition: all 0.3s;
	height: 3.2rem;
	line-height: 1.3rem;

	&:hover {
		background-color: #40a9ff;
	}

	&:active {
		background-color: #096dd9;
	}
}

.login-agreement {
	text-align: center;
	font-size: 0.9rem;
	color: #999;

	.register-link {
		color: #1890ff;
		cursor: pointer;
		text-decoration: none;
	}
}

.login-footer {
	margin-top: 2rem;
}

.divider {
	position: relative;
	text-align: center;
	color: #bfbfbf;
	font-size: 1rem;
	margin: 1.5rem 0;

	&:before,
	&:after {
		content: '';
		position: absolute;
		top: 50%;
		width: 45%;
		height: 1px;
		background-color: #e8e8e8;
	}

	&:before {
		left: 0;
	}

	&:after {
		right: 0;
	}
}

.social-login {
	display: flex;
	justify-content: center;
	gap: 1.2rem;
	margin-bottom: 1.5rem;
}

.social-button {
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 0.8rem 1rem;
	border: 1px solid #d9d9d9;
	border-radius: 4px;
	background-color: #fff;
	cursor: pointer;
	transition: all 0.3s;
	font-size: 1.2rem;

	&:hover {
		border-color: #1890ff;
		color: #1890ff;
	}

	&.wechat {
		color: #07c160;
		border-color: #07c160;
	}

	&.qq {
		color: #12b7f5;
		border-color: #12b7f5;
	}

	i {
		margin-right: 8px;
		font-size: 1.5rem;
	}
}

.register-hint {
	text-align: center;
	font-size: 1rem;
	color: #666;

	.register-link {
		color: #1890ff;
		text-decoration: none;
		margin-left: 5px;
		cursor: pointer;

		&:hover {
			text-decoration: underline;
		}
	}
}

/* 响应式设计 */
@media (max-width: 992px) {
	.login-content {
		flex-direction: column;
	}

	.login-left {
		display: none;
	}

	.login-right {
		width: 100%;
		padding: 20px;
	}
}
</style>
